package com.soriya.nestlive.common;

import com.soriya.nestlive.constant.CommonConst;
import com.soriya.nestlive.util.RedisUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AdminInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        if (!request.getRequestURI().startsWith("/admin")) return HandlerInterceptor.super.preHandle(request, response, handler);

        if ("/admin/sys/login".equals(request.getRequestURI())) {
            // 后台管理登录接口
            return HandlerInterceptor.super.preHandle(request, response, handler);
        }

        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) return false;

        Integer adminId = RedisUtil.get(CommonConst.ADMIN_TOKEN_PREFIX + token);

        GlobalThreadLocal.put("adminId", adminId);

        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
